package com.shop.sso.app.sercive.impl;

import com.shop.Result.LdbzResult;

import com.shop.dao.UserDao;
import com.shop.pojo.LdbzUser;
import com.shop.sso.app.sercive.UserApi;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.logging.Logger;


@Service
public class UserServiceImpl implements UserApi {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(UserServiceImpl.class);
    private static final Integer RANDON_NUMBER = 50000;
    private static final String IS_NICK_ENGAGED = "nick";
    private static final String IS_EMATL_ENGAGED = "email";//邮箱
    private static final String EMAIL_LOGIN_CODE = "EMAIL_LOGIN_CODE";

    private final String REDIS_KEY_VERIFYCODE = "VERIFYCODE";

    /*@Reference(version = Const.LDBZ_SHOP_REDIS_VERSION)
    private JedisClient jedisClient;*/

    /**
     * redis中session过期时间
     */
    @Value("${redisKey.expire_time:1800}")
    private Integer EXPIRE_TIME;

    /**
     * 注册成功之后跳转URL
     */
    @Value("${login.success_url}")
    private String SUCCESS_URL;

    @Autowired
    private UserDao userDao;
    @Override
    public LdbzResult login(LdbzUser user) {
        //判断用户与密码是否为空
        if (user == null ||
                //StringUtils是针对字符串操作的一个工具类，isEmpty方法是判断字符串是否为空
                StringUtils.isEmpty(user.getUsername()) ||
                StringUtils.isEmpty(user.getPassword())){
            return LdbzResult.build(500,"用户名与密码不能为空");
        }
        //判断用户名是否存在
        LdbzUser auser = userDao.selectByUser(user);
        if (auser == null){
            return LdbzResult.build(404,"用户名不存在");
        }
        //判断用户名或密码是否正确          调用DigestUtils算法工具类对密码进行加密
        if (!user.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))) {
            return LdbzResult.build(401,"用户名或密码错误");
        }
        //UUID是生成十六进制数的工具类,replaceAll方法是在字符串中用一些字符串替换一些字符或替换一个正则表达式字串
        //String token = UUID.randomUUID().toString().replaceAll("-","");
        //String key = Const.REDIS_KEY_USER_SESSION + token;//REDIS_KEY_USER_SESSION是在用户登录成功后放入redis中的key
        return LdbzResult.ok(user);
    }

    @Override
    public LdbzUser token(String token) {
        return null;
    }


    //注销
    @Override
    public LdbzResult logout(String token,String callback) {
        //调用isNotBlank判断callback是否是空，200成功，400没有此token500系统异常
        if (StringUtils.isNotEmpty(callback)){
            return LdbzResult.ok(callback);
        }else {
            if (callback==null){
                return LdbzResult.build(400,"没有登录");
            }
        }
        return LdbzResult.ok();
    }

    @Override
    public String validateUser(String isEngaged, String regName, String email) {
        return null;
    }

    @Override
    public String register(String regName, String pwd, String pwdRepeat, String emailCode, String uuid, String authCode, String s) {
        return null;
    }
}
